package com.example.pharmacy.controller;

import com.example.pharmacy.model.*;
import com.example.pharmacy.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-21
 * Time: 17:24
 */
@RestController
@RequestMapping("/drug")
public class DrugController {
    @Autowired
    private DrugService drugService;

    /**
     * 分页查询药品
     * @param pageRequest
     * @return
     */
    @RequestMapping("/getDrugsByPage")
    public Result getDrugsByPage(PageRequest pageRequest) {
        if (pageRequest.getCurrentPage() < 1 || pageRequest.getPageSize() < 0) {
            return Result.fail("翻页参数校验失败");
        }
        PageResult<DrugInfo> pageResult = drugService.getDrugsByPage(pageRequest);
        return Result.success(pageResult);
    }

    /**
     * 查询药品细节
     * @param drugId
     * @return
     */
    @RequestMapping("/getDrugDetail")
    public DrugInfo getDrugDetail(Integer drugId) {
        if (drugId == null || drugId <= 0) {
            return null;
        }
        return drugService.getDrugDetail(drugId);
    }

    /**
     * 分类查找药品
     *
     * @return
     */
    @RequestMapping("/getDrugByCategory")
    public Result getDrugsByCategory() {

        List<CategoryResult> categoryResults = drugService.getDrugsByCategory();
        return Result.success(categoryResults);
    }

    /**
     * 添加药品
     * @param drugInfo
     * @return
     */
    @RequestMapping("/addDrug")
    public Result addDrug(DrugInfo drugInfo) {
        if (drugInfo == null
                || drugInfo.getDrugName() == null
                || drugInfo.getEfficacy() == null
                || drugInfo.getInventory() == null
                || drugInfo.getClassId() == null
                || drugInfo.getPrice() == null
                || drugInfo.getValidity() == null) {
            return Result.fail("药品信息有空值");
        }
        Integer result = drugService.addDrug(drugInfo);
        if (result > 0) {
            return Result.success(true);
        }
        return Result.fail("添加失败");
    }
    /**
     * 更新药品
     * @param drugInfo
     * @return
     */
    @RequestMapping("/updateDrug")
    public Result updateDrug(DrugInfo drugInfo) {
        Integer ret = drugService.updateDrug(drugInfo);
        if (ret == 0) {
            return Result.fail("更新失败");
        }
        return Result.success("");
    }
    @RequestMapping("/selectDrugByName")
    public Result selectDrugByName(String drugName) {
        if (drugName == null) {
            return Result.fail("请输入药品名");
        }
        List<DrugInfo> drugInfo = drugService.selectDrugByName(drugName);
        if (drugInfo == null) {
            return Result.fail("暂无该药品");
        }
        return Result.success(drugInfo);
    }
}


